package leeon.bbsbrower.views;

import leeon.bbsbrower.actions.Config;
import leeon.bbsbrower.util.BrowerImages;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;

public class ImageDialog extends Dialog {
	
	private String[] name = null;  
	
	/**
	 * Create the dialog
	 */
	public ImageDialog(Shell parentShell, String[] name) {
		super(parentShell);
		this.name = name;
	}
	
	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	protected Control createDialogArea(Composite parent) {		
		final Canvas canvas = new Canvas(parent, SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL);
		
		GridLayout layout = new GridLayout();
		layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
		layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
		layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
		layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
		canvas.setLayout(layout);
		canvas.setLayoutData(new GridData(GridData.FILL_BOTH));
		
        final Image[] i = new Image[name.length];
        final int imageWidth[] = new int[name.length];
        final int imageHeight[] = new int[name.length];
        final Point origin = new Point (0, 0);
        final Point max = new Point(0, 0);
        
        int shellMaxWidth = Config.SCREEN_SIZE_W - 50;
        int shellMaxHeight = Config.SCREEN_SIZE_H - 50;
        for (int k = 0; k < name.length; k ++) {
        	try {
        		i[k] = BrowerImages.getImageFromPool(name[k]).createImage();
        	} catch (Exception ex) {
        		i[k] = BrowerImages.getImage(BrowerImages.ICONS_OBJ_SYSTRAY);
        		BrowerImages.removeImage(name[k]);
        		ex.printStackTrace();
        	}
			imageWidth[k] = i[k].getBounds().width;
			imageHeight[k] = i[k].getBounds().height;
			if (imageWidth[k] >= shellMaxWidth) {
				imageHeight[k] = (shellMaxWidth*imageHeight[k]/imageWidth[k]);
				imageWidth[k] = shellMaxWidth;
			}
			
			if (imageHeight[k] >= shellMaxHeight) {
				imageWidth[k] = (shellMaxHeight*imageWidth[k]/imageHeight[k]);
				imageHeight[k] = shellMaxHeight;
			}
						
			max.y += imageHeight[k];
			max.x = (max.x < imageWidth[k])?imageWidth[k]:max.x;
        }
        
        this.getShell().setSize(max.x+25, (max.y+35 < shellMaxHeight)?max.y+35:shellMaxHeight);
        
        canvas.addPaintListener(new PaintListener(){
			public void paintControl(PaintEvent e) {
				int y = origin.y;
				for (int k = 0; k < name.length; k ++) {
					e.gc.drawImage(i[k], 0, 0, i[k].getBounds().width, i[k].getBounds().height, 
							origin.x, y, imageWidth[k], imageHeight[k]);
					y += imageHeight[k];
				}
			}			
		});
        
        final ScrollBar bar = canvas.getVerticalBar();
		bar.setMaximum(max.y);
		final Rectangle client = this.getShell().getClientArea();
		bar.setThumb(Math.min(max.y, client.height));
		
		bar.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				int selection = bar.getSelection();
				int destY = -selection - origin.y;				
				canvas.scroll(0, destY, 0, 0, max.x, max.y, false);
				origin.y = -selection;				
			}

			public void widgetDefaultSelected(SelectionEvent e) {}
			
		});
		
        canvas.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent e) {
				if (e.keyCode == SWT.ESC || e.keyCode == SWT.ARROW_LEFT) {
					ImageDialog.this.close();
				} else if (e.keyCode == SWT.PAGE_DOWN) {
					int selection = bar.getSelection()+client.height;
					bar.setSelection(selection);
					bar.notifyListeners(SWT.Selection, null);
				} else if (e.keyCode == SWT.PAGE_UP) {
					int selection = bar.getSelection()-client.height;
					bar.setSelection(selection);
					bar.notifyListeners(SWT.Selection, null);
				} else if (e.keyCode == SWT.ARROW_DOWN) {
					int selection = bar.getSelection()+10;
					bar.setSelection(selection);
					bar.notifyListeners(SWT.Selection, null);
				} else if (e.keyCode == SWT.ARROW_UP) {
					int selection = bar.getSelection()-10;
					bar.setSelection(selection);
					bar.notifyListeners(SWT.Selection, null);
				}
			}

			public void keyReleased(KeyEvent e) {}			
		});
        
		
		return canvas;
	}
	
	
    /**
     * 设定窗口的标题和图片
     */
    protected void configureShell(Shell shell) {
    	super.configureShell(shell);
    	shell.setText("图片浏览");
    }
	
		

	/**
	 * 去掉下面的button bar
	 */
	protected Control createButtonBar(Composite parent) {
		return null;
	}
	
	/**
	 * Return the initial size of the dialog
	 */
	protected Point getInitialSize() {
		return new Point(800, 600);
	}
	
}
